// pages/shoppingcart/shoppingcart.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 调用云函数
    var userid=app.globalData.openid
    wx.cloud.callFunction({
      name: 'shoppingcart',
      data: { id: userid },
      success: res => {
        let { data } = res.result
        console.log(res.result)
        this.setData({
          shoppingcart:data
        })
        //
      },
      fail: err => {
        console.error('[云函数] [shoppincart] 调用失败', err)
      }
    })
  },
  pay: function () {
    //wx.navigateTo({ url: '../pay/pay' })
    wx.navigateTo({ url: '../fenlei/fenlei' })
  },
  jump: function (event) {
    var bookid=event.currentTarget.dataset.bookid
    wx.navigateTo({ url: '../detail/detail?id='+bookid })
  },
  delete:function(event){
    var bookid=event.currentTarget.dataset.bookid
    const userid = app.globalData.openid
    const db = wx.cloud.database()
    db.collection('shoppingcart').where({
      bookid:bookid,
      _openid:userid
    }).remove({
      success: res => {
        console.log('delete success')
        this.onShow()
      }
    })
  },
  buy:function(event){
    var bookid=event.currentTarget.dataset.bookid
    var seller_id=event.currentTarget.dataset.seller_id
    const userid = app.globalData.openid
    const db = wx.cloud.database()

    db.collection('book').where({
      _id:bookid,
      sellout:false
    }).get({
      success: res=> {
        console.log('sellout')
        console.log(res.data)
        if(res.data.length==0){
          wx.showToast({
            title: '库存不足',
            icon: 'none',
            duration: 1000,
            })
            this.delete(event)
        }
        else{
          db.collection('trade_record').add({
            data: {
              seller_id: seller_id,
              buyer_id: userid,
              bookid: bookid,
            },
            success: res => {
              this.delete(event)
              db.collection('book').doc(bookid).update({
                data:{
                  sellout: true
                },
                success:function(res){
                  console.log('update success')
                  console.log(res.data)
                }
                
              })
              wx.showToast({
                title: '购买成功',
                icon: 'succes',
                duration: 1000,
                })
            },
            fail: err => {
              console.error(err)
            }
          })
        }
      }
    })
    
   
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})